package dao.impl;

import dao.CityDao;
import entity.City;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.C3P0Util;

import java.sql.SQLException;
import java.util.List;

public class CityDaoImpl implements CityDao{

	@Override
	public List<City> getAllCityList(Integer proId) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		try {
			return qr.query("select * from city where proId = ?", 
					proId,new BeanListHandler<City>(City.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	@Override
	public City getCityByCityId(Integer CityId) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());	
		String sql="select name from city where id = ? ";
		City City=null;
		try {
			City=qr.query(sql, CityId, new BeanHandler<City>(City.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return City;
	}
	
}
